﻿using DX;
using DX.Behaviors;
using DX.Comm;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Windows.Forms;

namespace Tools
{
    public partial class MainForm : ViewForm, ILoadPluginsBehavior
    {
        public MainForm()
        {
            InitializeComponent();

        }
       

        void ILoadPluginsBehavior.AddPlugins(PluginProfile pluginProfile, EventHandler click)
        {
            if (pluginProfile == null) return;
            var pluginGroup = new PluginItem();
            pluginGroup.Name = pluginProfile.Name;
            pluginGroup.Title = pluginProfile.Title;
         
                EventHandler itemClick = (sender, e) =>
                {
                    if(this.ViewModel==null) return;
                    var viewModelType = Type.GetType(pluginProfile.Type);
                    var routFactory = this.ViewModel.Context.ServiceProvider.GetRequiredService<IFormRoutFactory>();
                    if (pluginProfile.AllowMultipleCreation)
                    {
                        routFactory.Show(viewModelType);
                    }
                    else
                    {
                        routFactory.FindViewShow(viewModelType);
                    }
                    click?.Invoke(sender, e);

                };
                pluginGroup.ItemDoubleClick += itemClick;
           
            flowLayoutPanel1.Controls.Add(pluginGroup);

        }
 
    }
}
